package org.csu.goodpartner.controller;

import com.alibaba.fastjson.JSON;
import org.csu.goodpartner.VO.AccountVO;
import org.csu.goodpartner.common.CommonResponse;
import org.csu.goodpartner.entity.Account;
import org.csu.goodpartner.entity.SignOn;
import org.csu.goodpartner.service.AccountService;
import org.csu.goodpartner.service.MsmService;
import org.csu.goodpartner.service.impl.MsmServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@Controller
@RequestMapping("/account")
public class AccountController {
    @Autowired
    private AccountService accountService;

    //账号注册
    @PostMapping("/register")
    @ResponseBody
    public CommonResponse<Account> register(@RequestBody AccountVO account)
    {
        return accountService.registerAccount(account.getName(),account.getPassword(),account.getPhone());
    }
    //一个账号
    @PostMapping("/oneAccount")
    @ResponseBody
    public CommonResponse<AccountVO> oneAccount(@RequestBody String id)
    {
        int id1=Integer.valueOf(id).intValue();
        return accountService.oneAccount(id1);
    }

    //账号
    @PostMapping("/login")
    @ResponseBody
    public CommonResponse<Account> login(@RequestBody SignOn signOn, HttpServletRequest request)
    {
        System.out.println("进入login");
        HttpSession session=request.getSession();
        //先判断状态
        int id1=Integer.valueOf(signOn.getUserid());
        CommonResponse data=accountService.login(id1,signOn.getPassword());
        if(data.isSuccess())
        {
            session.setAttribute("account",data.getData());
            System.out.println(((AccountVO)(session.getAttribute("account"))).getId());

        }
        return data;
    }

    //修改账号信息
    @PostMapping("/updateAccount")
    @ResponseBody
    public CommonResponse<AccountVO> update(@RequestParam String password,
                                            @RequestParam String tel,
                                            @RequestParam String email,
                                            @RequestParam String addr,
                                            @RequestParam String username,
                                            @RequestParam String IDCard,
                                            HttpSession session)
    {
        int id=((AccountVO)(session.getAttribute("account"))).getId();
        System.out.println(id);
        //更新操作
        return accountService.updateAccount(id,username,password,tel,IDCard,email,addr);

    }
    //退出登录
    @PostMapping("/logout")
    @ResponseBody
    public CommonResponse logout(HttpSession session)
    {
        session.setAttribute("account",null);
        return CommonResponse.createForSuccessMessage("退出成功");
    }

    //账号注销
    @PostMapping("/deleteAccount")
    @ResponseBody
    public CommonResponse deleteAccount(HttpSession session)
    {
        int id=((AccountVO)session.getAttribute("account")).getId();
        return accountService.deleteAccount(id);
    }

    //邮箱找回密码
    @GetMapping("/sendCode")
    public  void sendCode(Model model, HttpServletRequest request, HttpServletResponse resp) throws IOException {
        String mail = request.getParameter("email");
        System.out.println("email");
        MsmService msmService = new MsmServiceImpl();
        String code = msmService.getCode();
        if (msmService.sendEmail(mail, code, "jpetstore验证码")) {
            HttpSession session = request.getSession();
            session.setAttribute("code", code);
            //resp是 HttpServletResponse
            resp.setContentType("text/json");
            PrintWriter printWriter = resp.getWriter();
            //result是查询出来的东西
            String str = JSON.toJSONString("ok");
            printWriter.print(str);
            System.out.println(str);
            printWriter.flush();
            printWriter.close();
        } else {

            resp.setContentType("text/json");
            PrintWriter printWriter = resp.getWriter();
            //result是查询出来的东西
            String str = JSON.toJSONString("error");
            printWriter.print(str);
            System.out.println(str);
            printWriter.flush();
            printWriter.close();
        }
    }


}
